最近用到,記錄一下
首先先寫一支接收端,只是單純的把POST參數印出來
api.php
<?php
if ($_POST)
{
print_r($_POST) ;
}
?>
另外寫一支put.php
<?php
$post = array('name'=>'Joe', gender=>"male") ; //post
$host = 'localhost' ;
$port = 80 ;
$errno = 0 ; //錯誤碼會回存這裡。
$errstr = '' ; //錯誤資訊會回存這裡
$timeout = 5 ;
foreach ($post as $k => $v)
{
$strPost .= ('&'.$k.'='.$v) ;
}
$request.="POST ".'/sock/api.php'." HTTP/1.1\n";
$request.="Host: ".$host."\n";
$request.="Referer: ".$_SERVER["SCRIPT_URI"]."\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($strPost)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$strPost."\n";
$fp = fsockopen($host, $port, $errno, $errstr, $timeout) ;
fputs($fp, $request) ;
while ($result = fgets($fp, 1024))
{
if ($result == "\r\n")
{
break ; //表示表頭結束
}
}
while(!feof($fp))
{
$result .= fgets($fp, 128);
}
fclose($fp);
echo $result ;